/**
* 
* GUI: Graphical data
* Copyright (c) 2009 Ecole Centrale de Nantes IRCCyN  
* Classe: JData.java
* see <a href="{@docRoot}/"> for more information</a>
* 
* @author: Urunuela Richard
* @version 3.2
*/
package programme.GUI;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.util.Hashtable;
import java.util.Map;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.SwingConstants;
import javax.swing.tree.DefaultMutableTreeNode;

import org.jgraph.JGraph;
import org.jgraph.graph.CellView;
import org.jgraph.graph.CellViewFactory;
import org.jgraph.graph.CellViewRenderer;
import org.jgraph.graph.DefaultCellViewFactory;
import org.jgraph.graph.DefaultEdge;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.DefaultGraphModel;
import org.jgraph.graph.GraphConstants;
import org.jgraph.graph.GraphModel;
import org.jgraph.graph.VertexRenderer;
import org.jgraph.graph.VertexView;
import org.jgraph.*;
import org.jgraph.graph.DefaultCellViewFactory;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.VertexView;

import programme.GUILayout.GPCellViewFactory;
import programme.GUILayout.JGraphLayoutAlgorithm;
import programme.GUILayout.SugiyamaLayoutAlgorithm;
import programme.GUILayout.TreeLayoutAlgorithm;


/**
 * 
 * @author richard
 *
 */
public class JData extends JGraph implements CellViewFactory{
	/**
	 * 
	 */
	public static DefaultGraphCell createVertex(double x,
		double y, double w, double h, Color bg, boolean raised, DefaultGraphCell cell, String viewClass) {
		if (viewClass != null){
			System.out.println(cell.getAttributes());
			GPCellViewFactory.setViewClass(cell.getAttributes(), viewClass);
			System.out.println(cell.getAttributes());
		}
		// Set bounds
		GraphConstants.setBounds(cell.getAttributes(), new Rectangle2D.Double(
				x, y, w, h));
		// Set fill color
		if (bg != null) {
			GraphConstants.setGradientColor(cell.getAttributes(), bg);
			GraphConstants.setOpaque(cell.getAttributes(), true);
		}
		// Set raised border
		if (raised){
			GraphConstants.setBorder(cell.getAttributes(), BorderFactory
					.createRaisedBevelBorder());
		}
		else{
			// Set black border
			GraphConstants.setBorderColor(cell.getAttributes(), Color.black);
		}
		    // Add a Floating Port
		cell.addPort();
		return cell;
	}
	/**
	 * 
	 */	
	public static DefaultGraphCell createVertex(String name, double x,
		double y, double w, double h, Color bg, boolean raised) {
		// Create vertex with the given name
		DefaultGraphCell cell = new DefaultGraphCell(name);
		// Set bounds
		GraphConstants.setBounds(cell.getAttributes(), new Rectangle2D.Double(
				x, y, w, h));
		// Set fill color
		if (bg != null) {
			GraphConstants.setGradientColor(cell.getAttributes(), bg);
			GraphConstants.setOpaque(cell.getAttributes(), true);
		}
		// Set raised border
		if (raised){
			GraphConstants.setBorder(cell.getAttributes(), BorderFactory
					.createRaisedBevelBorder());
		}
		else{
			// Set black border
			GraphConstants.setBorderColor(cell.getAttributes(), Color.black);
		}
		    // Add a Floating Port
		cell.addPort();
		return cell;
	}
	/**
	 * 
	 */
	private void drawElement() {
		DefaultGraphCell[] cells = new DefaultGraphCell[8];
		cells[0] = createVertex(20, 20, 60, 30, Color.red, false, new DefaultGraphCell("hello ---"), "programme.JGraphEllipseView");
		cells[1] =   createVertex("Hello 2", 10, 50, 50, 20, Color.blue, false);
		cells[2] =   createVertex("Hello 3", 10, 100, 50, 20, null, false);
		DefaultEdge edge = new DefaultEdge();
		DefaultEdge edge2 = new DefaultEdge();
		edge.setSource(cells[0].getChildAt(0));
		edge.setTarget(cells[1].getChildAt(0));
		edge2.setSource(cells[0].getChildAt(0));
		edge2.setTarget(cells[2].getChildAt(0));
		cells[3] = edge;
		cells[4] = edge2;
		cells[5] =   createVertex("Hello 4", 10, 100, 50, 20, null, false);
		cells[6] =   createVertex("Hello 5", 10, 100, 50, 20, null, false);
		DefaultEdge edge3 = new DefaultEdge();
		edge3.setSource(cells[5].getChildAt(0));
		edge3.setTarget(cells[2].getChildAt(0));
		cells[7] = edge3;
		final JGraphLayoutAlgorithm controller = new  SugiyamaLayoutAlgorithm();
		Object[] cells2 =  DefaultGraphModel.getAll(this.getModel());
		 this.getGraphLayoutCache().insert(cells);
		if (cells != null && cells.length > 0) {
			JGraphLayoutAlgorithm.applyLayout(this, controller,
					cells, null);
		}
		
		
	}
	 /**
	  * 
	  * @param model
	  */
	public JData(GraphModel model){
		super(model);
		this.getGraphLayoutCache().setFactory(new GPCellViewFactory());
		// Control-drag should clone selection
		this.setCloneable(true);
		// Enable edit without final RETURN keystroke
		this.setInvokesStopCellEditing(true);
		// When over a cell, jump to its default port (we only have one, anyway)
		this.setJumpToDefaultPort(true);
	}
	/**
	 * 
	 * @param name
	 * @param matrice_adjacence
	 * @param model
	 */
	public JData(Vector name, Vector matrice_adjacence, GraphModel model) {
		super(model);
		drawElement(name,matrice_adjacence);
		this.setEnabled(false);
	}
	/**
	 * 
	 * @param name
	 * @param matrice_adjacence
	 */
	private void drawElement(Vector name, Vector matrice_adjacence) {
		// TODO Auto-generated method stub
		int size = name.size();
		Vector tmp = new Vector();
		for (int i=0; i<size;i++){
			String name2 = name.get(i).toString();
			DefaultGraphCell crt=null ;
			if (name2.compareToIgnoreCase("DATA") ==0){
					crt = createVertex("", 10, 10, 80, 5,Color.red, false);
			}
			else crt = createVertex(name2, 10, 100, 80, 20, null, false);
			tmp.add(crt);
			//Parcour du graphe d'ajacence
		}
		for (int i=0; i<size;i++){
			int colonne[] = (int []) matrice_adjacence.get(i);
			for (int j=0; j< size ; j++){
				int value = colonne[j];
			    if(value == 1){
					DefaultEdge edge = new DefaultEdge();
				    DefaultGraphCell source =  (DefaultGraphCell) ((DefaultMutableTreeNode) tmp.get(i));
					DefaultGraphCell dest =   (DefaultGraphCell) ((DefaultMutableTreeNode) tmp.get(j));
					edge.setSource(source.getChildAt(0));
					edge.setTarget(dest.getChildAt(0));
					tmp.add(edge);
				}
				
			}
			
		}
		DefaultGraphCell[] cells = new DefaultGraphCell[tmp.size()];
		for (int i=0; i< tmp.size() ; i++){
			cells[i] =(DefaultGraphCell) tmp.get(i); 
		}
		final JGraphLayoutAlgorithm controller = new  SugiyamaLayoutAlgorithm();
		//final JGraphLayoutAlgorithm controller = new  TreeLayoutAlgorithm();
	    this.getGraphLayoutCache().insert(cells);
		if (cells != null && cells.length > 0) {
			JGraphLayoutAlgorithm.applyLayout(this, controller,
					cells, null);
		}
	}
	/**
	 * 
	 */
	public CellView createView(GraphModel arg0, Object arg1) {
		// TODO Auto-generated method stub
		return null;
	}
	

}
